#include "../src/EventFileDriver.h"
#include "../src/UserTable.h"

#include <stdio.h>

static EventList *
create_test_list(void)
{
	EventList *head;

	head = event_list_new();

	for (euint32 i = 1; i < 100; i += 3)
		event_list_add_data(head, EPTR((euint64)(i + 1900000)),
				    EVENT_TYPE_UINT32, NULL);

	return head;
}

int
main(void)
{
	UserTable *table;
	EventList *list;
	int	   status;
	char	   buf[15];
	EventHash *hash[2];

	table	= user_table_new();
	hash[0] = __user_table_get_hash_table(table, 0);
	hash[1] = __user_table_get_hash_table(table, 1);

	event_file_driver_set_file(EVENT_FILE_DRIVER(table), "usertable.efd");
	status = event_file_driver_loader(EVENT_FILE_DRIVER(table));

	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("打开文件失败(code: %d)，尝试创建新文件 ......\n",
		       status);
		status = event_file_driver_create(EVENT_FILE_DRIVER(table));
		if (status != EVENT_FILE_DRIVER_SUCCESS) {
			printf("创建文件失败！\n");
			event_object_unref(table);
			return status;
		}
	} else {
		printf("打开文件成功！\n");
	}

	event_hash_print_status(hash[0]);
	event_hash_print_status(hash[1]);

	/* 打印一条测试用户数据
        UserItem *item;
        item = __user_table_from_id_get_item(table, 1000001);
        __user_item_print(item); */

	/* 添加测试用例 */
	list = create_test_list();
	for (int i = 0; i < 300; ++i) {
		EventString *pw, *un;
		sprintf(buf, "%08d", 10000000 + i);
		pw = event_string_new(buf);
		sprintf(buf, "user%d", i);
		un = event_string_new(buf);
		__user_table_add_item(table, (15133227735 + i * 12), pw, un,
				      1000000 + i, list);
		event_object_unref(pw);
		event_object_unref(un);
	}
	event_object_unref(list);

	/* 打印一条测试用户数据
        item = __user_table_from_id_get_item(table, 1000001);
        __user_item_print(item); */

	/* 写入用户数据测试 */
	status = event_file_driver_dump(EVENT_FILE_DRIVER(table));
	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("写入用户数据失败！\n");
		event_object_unref(table);
		return status;
	} else {
		printf("写入成功！\n");
	}

	//event_hash_print_status(hash[0]);
	//event_hash_print_status(hash[1]);

	event_object_unref(table);

	return 0;
}